package awk.etl;

import awk.etl.scripts.DynamicClass;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 执行ETL,
 *
 * @author
 */
public class EtlEngineer {
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        String scripts = "package com.cloudwise34.lcap.etl.asdfs;"+
                "import com.cloudwise.lcap.etl.etlcommon.scripts.ScriptBase;" +
                "import java.util.List;\n" +
                "import java.util.Map;\n" +
                "\n" +
                "public class Mycode extends ScriptBase{\n" +
                "\n" +
                "    public static Object execute(String[] dataSetIds,Map<String ,Object> params) {\n" +
                "        List<Map<String, Object>> dataSet0 = executeDs(dataSetIds[0],params);\n" +
                "        List<Map<String, Object>> dataSet1 = executeDs(dataSetIds[1],params);\n" +
                "        return executeFun(\"ekko.converge_join\", dataSet0, dataSet1);\n" +
                "    }\n" +
                "}\n";
        String fullClassName = "com.cloudwise34.lcap.etl.asdfs.Mycode";
        String[] dataSetIds = {"235365645", "457657986"};
        EtlContext context = EtlContext.builder().scripts(scripts).fullClassName(fullClassName).dataSetIds(dataSetIds)
                .params(new HashMap<>()).build();
        Object result = execute(context);

        System.out.println("result:" + result);
    }


//    public static String getClassName(String scripts){
//        String[] split = scripts.split(";");
//    }


    public EtlEngineer() {
    }

    /**
     * @param context
     * @return
     */
    public static Object execute(EtlContext context) throws Exception {
        DynamicClass dynamicClass = new DynamicClass(context.getScripts(), context.getFullClassName(), new ArrayList<>());
        //每次都是new新的ClassLoader对象
        Class<?> aClass = dynamicClass.compilerFromSource();
        Class[] cArg = new Class[]{String[].class, Map.class};
        Method execute = aClass.getDeclaredMethod("execute", cArg);
        return execute.invoke(aClass, new Object[]{context.getDataSetIds(), context.getParams()});
    }


}
